//
// Created by ins on 2021/2/28.
//

#include "MapDao.h"
#include "MapBean.h"
#include <kisql.h>
#include "tool/Util.h"
#include <iostream>
#include <string>
#include <list>

using std::string;
using std::list;
static kisql *db;
static MYSQL_RES *result;
static MYSQL_ROW row;

MapDao *MapDao::sInstance = new MapDao();
MapDao::MapDao() {
    db = kisql::getInstance();
}
MapDao::~MapDao() {}
MapDao* MapDao::getInstance() {return sInstance;}

void MapDao::getMap(int mid, MapBean &retMap) {

    char sql[1024] = {0};
    sprintf(sql, R"(select * from map, map_exits where map.id = "%d" and map_exits.id = "%d")",
            mid, mid);

    if(mysql_query(db->getMysql(), sql)){
        perror("mysql query err");
        return;
    } else{ //select success
        result = mysql_store_result(db->getMysql());
        if(result){
            int num_rows = mysql_num_rows(result);

            for(int i = 0; i < num_rows; i++) {
                row = mysql_fetch_row(result);
                if (row == nullptr) {
                    break;
                }

                retMap.setId(row[0]);
                retMap.setArea(row[1]);
                retMap.setPname(row[2]);
                retMap.setName(row[3]);
                retMap.setGid(row[4]);
                retMap.setNpc(row[5]);
                retMap.setMgtime(row[6]);
                retMap.setMs(row[7]);
                retMap.setDesc(row[8]);
                retMap.setBoss(row[9]);
                retMap.setQy(row[10]);
                retMap.setPlayerinfo(row[11]);
                retMap.setIspvp(row[12]);

                list<MapExitBean> mapExits;
                mapExits.push_back(new MapExitBean(row[16],"",""));
                mapExits.push_back(new MapExitBean(row[17],"",""));
                mapExits.push_back(new MapExitBean(row[18],"",""));
                mapExits.push_back(new MapExitBean(row[19],"",""));
                mapExits.push_back(new MapExitBean(row[20],"",""));
                mapExits.push_back(new MapExitBean(to_string(mid),"",""));
                mapExits.push_back(new MapExitBean(row[21],"",""));
                mapExits.push_back(new MapExitBean(row[22],"",""));
                mapExits.push_back(new MapExitBean(row[23],"",""));
                mapExits.push_back(new MapExitBean(row[24],"",""));
                mapExits.push_back(new MapExitBean(row[25],"",""));
                mapExits.push_back(new MapExitBean(row[26],"",""));
                mapExits.push_back(new MapExitBean(row[27],"",""));
                retMap.setMapExits(mapExits);
                //cout << "retMap: " << retMap.toString() << endl;
            }

        }
    }
}

void MapDao::getMapExitInfo(list<MapExitBean> &mapExits) {

    for(MapExitBean &map : mapExits) {
        if (map.getMid() != "0") {
            string sql = "select pname, name from map where id = " + map.getMid();

            if (mysql_query(db->getMysql(), sql.c_str())) {
                perror("mysql query err");
                return;
            } else { //select success
                result = mysql_store_result(db->getMysql());
                if (result) {

                    int num_rows = mysql_num_rows(result);

                    for (int i = 0; i < num_rows; i++) {
                        row = mysql_fetch_row(result);
                        if (row == nullptr) {
                            break;
                        }
                        map.setPname(row[0]);
                        map.setName(row[1]);
                    }
                }
            }
        }
    }
    //db->closeDB();
}

bool MapDao::updateMGtime(int mid) {

    char sql[1024] = {0};
    sprintf(sql, R"(update map set mgtime= "%s" where mid = "%s")",
            Util::getTime().c_str(), to_string(mid).c_str());

    if(mysql_query(db->getMysql(), sql)){
        perror("mysql query err");
        return false;
    }else{
        if(mysql_field_count(db->getMysql()) == 0){
            return true;
        }
    }
    return false;
}
